#!/usr/bin/python env
# -*- coding: utf-8 -*-
# @Time   :2018/4/29 09:47
# @Author :huchao
from BuMenSDKclass import *

# 以下是测试代码

star = 200
end = star + 5
file = "bt"


def main():
    '''测试程序主函数'''
    # createuse()
    # assetissuer()
    #
    # # assetNewissure()
    # #
    # BuMen.writeLog(file)
    # 交易查询
    try:
        assetSend()
        print("*"*100)
        checkforaddress()
        print("*"*100)
        checkforhash()
        print("*"*100)
        assetIssrueSure()
        print("*"*100)
        trade_noCheck()
    except json.decoder.JSONDecodeError as err:
        print(err, "出问题啦")


def checkforaddress():
    '''bubiaddress check'''
    bumeninit()
    BuMen.readUseInfoInFile(file)
    for user in BuMen.getUserList():
        Str = user.TXcheck()
        print(Str)


def trade_noCheck():
    '''更具交易号查询'''
    bumeninit()
    BuMen.readUseInfoInFile(file)
    for asset in BuMen.getAssetList():
        Str = asset.trade_noCheck()
        print(Str)


def checkforhash():
    '''依据hash值查询'''
    bumeninit()
    BuMen.readUseInfoInFile(file)
    for asset in BuMen.getAssetList():
        Str = asset.bc_hashCheck()
        print(Str)


def assetIssrueSure():
    '''查询资产发行状态'''
    bumeninit()
    BuMen.readUseInfoInFile(file)
    for asset in BuMen.getAssetList():
        print(asset.issueStatus())

def assetSend():
    '''同步方式资产转移交易'''
    bumeninit()
    BuMen.readUseInfoInFile(file)

    toUser = BuMen.getUserList()[1]
    Asset = BuMen.getAssetList()[0]
    Asset.Transaction(toUser,1000)
    BuMen.writeLog(file)




def assetNewissure():
    '''资产增发'''
    bumeninit()
    # (1)读取用户信息
    BuMen.readUseInfoInFile(file)
    # (1)
    for user in BuMen.getUserList():
        # (1)
        for asset in BuMen.getAssetList():
            print(type(asset), asset, 34234)
            if isinstance(asset, str):
                break
            asset.assetNewIssue(user, 20000)  # 资产增发2万
    for asset in BuMen.getAssetList():
        print(asset)


def assetissuer():
    '''测试用户激活和资产发行'''
    # bumeninit() #初始化BuMen类

    # BuMen.readUseInfoInFile(file) #加载用户信息
    # 配置BuMenAsset 初始化数据
    assetdate = {
        "asset_name": "黑耳朵资产",
        "asset_unit": "朵",
        "asset_amount": "21300",
        "metadata": "{\"asset_type\":\"10600\",\"asset_unit_code\":\"15\",\"asset_description\":\"xxxxxxxxxxx\"}"
    }
    Assert = BuMenAsset(**assetdate)  # BuMenAsset 对象初始化
    for use in BuMen.getUserList():
        Assert.assetIssue(use)  # 发布BuMenAsset资产
    ("这里创建了资产", BuMen.getAssetList())
    # BuMen.writeLog("bumenuser.log") #把更新的用户数据写入到文件中


def getUserinfoTest():
    bumeninit()  # 初始化BuMen类

    BuMen.readUseInfoInFile(file)  # 加载用户
    for use in BuMen.getUserList():
        (use.getUseinfo())  # 测试返回用户详情接口

        # (use.getUserIscreate()) # 测试返回用户注册状态接口


def changepassword():
    '''重置账户测试用例'''
    bumeninit()  # 初始化BuMen类
    # 加载用户
    BuMen.readUseInfoInFile(file)
    # 修改用户密码
    for use in BuMen.getUserList():
        use.updatePassword("heijingyu")
        # 把更新后的用户信息写入到文件中
        # BuMen.writeLog("bumenuser.log")


def bumenuserloading():
    '''布萌sdk本地文件系统测试用例'''
    bumeninit()

    # 读取文件中的用户数据循恢复BuMenUser对象
    BuMen.readUseInfoInFile(file)
    for use in BuMen.getUserList():
        print([use.getUserArryList()])
        # 在把读取到到的用户信息写入到文件中
        # BuMen.writeLog("bumenuser.log")


def bumeninit():
    '''
    初始化BuMen类
    :return:
    '''
    bumeninfo = {
        "client_id": "1d60edfa67af7e1ce4ac1dab40577e6c",
        "client_secret": "803da2e4d8a8b96726c96a04b912b5b5",
        "url": "https://testapi.bumeng.cn",
        "grant_type": "client_credentials",
    }
    BuMen.setClassArry(**bumeninfo)  # 初始化布萌类


def createuse():
    '''布萌用户创建测试用例
    
    同时创建循环创建10个用户
    '''

    bumeninit()  # 初始化BuMen类

    # 循环创建10个用户
    for x in range(star, end):
        usedata = {
            "username": "xiaokeai00" + str(x),
            "password": "123456",
            "nickname": "黑金鱼" + str(x),
            "metadata": "说实话么有什么好写的 就这样把"
        }
        # 初始化BuMenUser类
        BU = BuMenUser(**usedata)
        # 依据初始化的BuMenUser 创建用户
        (BU.__repr__())
        BU.createUser()

        # BuMen.writeLog("bumenuser.log")


if __name__ == '__main__':
    main()
